//
//  NRLSVMForScreenViewController.m
//  NaRiLSVMT
//
//  Created by zhangmeng on 14-8-25.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "NRLSVMForScreenViewController.h"
#import "UIImageView+AFNetworking.h"
#import "TcpClient.h"
#import "MBProgressController.h"
#import "Network_Operation.h"


extern  BOOL    isFlowProjectPlay;//是哪个模块进行的双击大图操作(流程控制or屏幕预览)

@interface NRLSVMForScreenViewController ()

@end

@implementation NRLSVMForScreenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.hostIdarr = [NSMutableArray arrayWithArray:[self.hostId componentsSeparatedByString:@","]];
    self.zoomScroll = [[MRZoomScrollView alloc] init];
    self.zoomScroll.frame = Frame(14,62,Screen_Width-28,606);
    self.zoomScroll.imageData = [[DownloadImage sharedInstance] getUrlImage:StringFromId(self.screenData[@"templateId"]) Block:^(UIImage *image){
        self.zoomScroll.imageData = image;
        [self.zoomScroll initImageView];
                              }];
    if(self.zoomScroll.imageData){
         [self.zoomScroll initImageView];
    }
    [[TimerDownImage sharedInstance] downCallBackData:StringFromId(self.screenData[@"templateId"]) CallBack:^(UIImage *image){
        [self.zoomScroll.imageView setImage:image];
    }];
    
    self.zoomScroll.aceDrawView.zoomX = [self.screenData[@"width"] intValue]/((Screen_Width-28));
    self.zoomScroll.aceDrawView.zoomY = [self.screenData[@"width"] intValue]/((Screen_Width-28));
    
    self.zoomScroll.aceDrawView.screenWidth = [self.screenData[@"width"] intValue];
    self.zoomScroll.aceDrawView.screenHeight = [self.screenData[@"height"] intValue];
    //self.zoomScroll.aceDrawView.hostId = self.hostId;
    self.zoomScroll.aceDrawView.hostIdarr = [NSMutableArray arrayWithArray:self.hostIdarr];
    
    [self.zoomScroll.aceDrawView configure];
    [self.view addSubview:self.zoomScroll];
    
    self.drawView.frame = Frame(0,680,730,80);
    [self.view addSubview:self.drawView];
    self.drawView.hidden = YES;
    
    self.chooseColour = [[UIImageView alloc] initWithFrame:Frame(20,20,45,45)];
    UIImage *image = [Common drawImageSize:CGSizeMake(52,70) Color:ClearColor];
    [self.chooseColour setImage:image];
    self.chooseColour.layer.borderColor = [UIColor orangeColor].CGColor;
    self.chooseColour.layer.borderWidth = 3.0;
    self.chooseWidth = [[UIImageView alloc] initWithFrame:Frame(359,10,52,70)];
    [self.chooseWidth setImage:[UIImage imageNamed:@"forscreen_follow.png"]];
    
    [self.drawView addSubview:self.chooseColour];
    [self.drawView addSubview:self.chooseWidth];
    self.proView = [[PromtViewController alloc] init];
    self.proView.view.frame = Frame(205, 163, 617, 417);
    self.proView.delegate = self;
    self.proView.screenId = self.forscreenId;
    self.proView.templeted = StringFromId(self.screenData[@"templateId"]);
    
    self.promeView = [[UIView alloc] init];
    self.promeView.frame = self.view.frame;
    self.promeView.backgroundColor = [UIColor blackColor];
    self.promeView.alpha = 0.3;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappro:)];
    [self.promeView addGestureRecognizer:tapGesture];
    
    NSString *name = [NSString stringWithFormat:@"%@ %@ %@",self.screenNum,@">",self.screenData[@"name"]];
    self.screenId.text = name;
    
    if(isFlowProjectPlay == NO)
    {
        NSString *name = [NSString stringWithFormat:@"%@ %@ %@",self.screenNum,@">",self.screenData[@"name"]];
        self.screenId.text = name;
    }
    else
    {
        NSString    *tmpName = self.screenData[@"name"];
        NSInteger index = [tmpName rangeOfString:@"【"].location;
        tmpName = [tmpName substringWithRange:NSMakeRange(0, index)];
        
        NSString *name = [NSString stringWithFormat:@"%@  %@  %@",self.screenNum,@">",tmpName];
        self.screenId.text = name;
    }
    self.colorPanArr = @[@"#c80e0c",@"#e1da00",@"#00bae0",@"#00e22f",@"#5300b9",@"#d6088a"];
    self.segmentWidth = 3;
    self.segmentColor = 0;
    [AddNoticeObserver(NoticeKey_EventNull){
        self.talkbtnflag = YES;
        self.forBtnscreen = NO;
         [self.talkBtn setBackgroundImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
        if(!self.panClickscreen && !self.forBtnscreen){
            self.zoomScroll.aceDrawView.hidden = NO;
            self.panClickscreen = NO;
            self.forBtnscreen = NO;
        }
        //隐藏交互按钮
        [self.zoomScroll.aceDrawView forButtonHidden:YES];
        if(self.zoomScroll.aceDrawView.dropListView.isOpen){
            [self.zoomScroll.aceDrawView.dropListView hideExtendedChooseView];
        }
        
    }];
    self.talkbtnflag = YES;
    self.editPanflag = NO;
 
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated
{



}
-(void)viewWillDisappear:(BOOL)animated
{

}
-(void)viewDidUnload
{
    [super viewDidUnload];
    self.proView = nil;
    self.imageView = nil;
    self.promeView = nil; //覆盖的view
    self.chooseColour = nil;
    self.chooseWidth = nil;
    self.zoomScroll = nil; // 放大的zoom
    self.screenData = nil; //屏幕的缩略图的信息
    self.colorPanArr = nil; // 画笔的颜色
    self.screenNum = nil;  //屏幕的名称
    self.forscreenId = nil;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    self.proView = nil;
    self.imageView = nil;
    self.promeView = nil; //覆盖的view
    self.chooseColour = nil;
    self.chooseWidth = nil;
    self.zoomScroll = nil; // 放大的zoom
    self.screenData = nil; //屏幕的缩略图的信息
    self.colorPanArr = nil; // 画笔的颜色
    self.screenNum = nil;  //屏幕的名称
    self.forscreenId = nil;
    // Dispose of any resources that can be recreated.
}
-(IBAction)backBtnclick:(id)sender
{
    
    [self.zoomScroll.aceDrawView clear];
    //发送清空命令
    TcpClient *tcp = [TcpClient sharedInstance];
    for(NSString *host in self.hostIdarr){
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [tcp writeString:[Common tcpStartEnDevDrastring:[host intValue] Metuod:@"DeviceClearDraw"]];
        });
    }
   // TcpClient *tcp = [TcpClient sharedInstance];
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for(NSString *host in self.hostIdarr){
            [tcp writeString:[Common tcpStartEnDevDrastring:[host intValue] Metuod:@"DeviceEndMark"]];
        }
    });
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(IBAction)messageBtnclick:(id)sender
{
    //弹出提示框
     UIButton *btn = (UIButton *)sender;
    [btn setImage:[UIImage imageNamed:@"forscreen_messageclick.png"] forState:UIControlStateNormal];
    [self.view addSubview:self.promeView];
    [self.view addSubview:self.proView.view];
    [self.proView showViewblock:StringFromId(self.screenData[@"templateId"]) Name:self.screenData[@"name"]];
}
-(IBAction)talkBtnclick:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    static int i = 0;
    if(self.talkbtnflag){
      //交互
        self.talkbtnflag = NO;
        self.zoomScroll.aceDrawView.drawingMode = DrawingModeNoneing;
        self.zoomScroll.panflag = NoPan;
        self.zoomScroll.aceDrawView.hidden = NO;
        //显示交互按钮
        [self.zoomScroll.aceDrawView forButtonHidden:NO];
        self.forBtnscreen = YES;
        if(self.panClickscreen){
            self.zoomScroll.aceDrawView.drawingMode = DrawingModePainting;
            self.zoomScroll.panflag = Pan;
        }
        [btn setImage:[UIImage imageNamed:@"forscreen_talkclick.png"] forState:UIControlStateNormal];
        TcpClient *tcp = [TcpClient sharedInstance];
        if(!tcp.asyncSocket.isConnected)
        {
            [[MBProgressController sharedInstance] showTipsOnlyText:@"TCP连接出错" AndDelay:1.5];
            return;
        }
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
             //for(NSString *host in self.hostIdarr){
            [tcp writeString:[Common hostidtostring:[self.hostIdarr[0] intValue] Metuod:@"GetEvents"]];
            // }
        });
        i = 1;
    }
    else{
    //非交互
        self.talkbtnflag = YES;
        self.forBtnscreen = NO;
        if(!self.panClickscreen && !self.forBtnscreen){
            self.zoomScroll.aceDrawView.hidden = NO;
            self.panClickscreen = NO;
            self.forBtnscreen = NO;
        }
        [btn setImage:[UIImage imageNamed:@"forscreen_talk.png"] forState:UIControlStateNormal];
        //隐藏交互按钮
        [self.zoomScroll.aceDrawView forButtonHidden:YES];
        if(self.zoomScroll.aceDrawView.dropListView.isOpen){
            [self.zoomScroll.aceDrawView.dropListView hideExtendedChooseView];
        }
        i = 0;
    }

}
//点击画笔
-(IBAction)editBtnclick:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    //static int i = 0;
    
    if(!self.editPanflag){
        self.drawView.hidden = NO;
        self.zoomScroll.aceDrawView.drawingMode = DrawingModePainting;
        self.zoomScroll.panflag = Pan;
        self.zoomScroll.pinchGestureRecognizer.enabled = NO;
        self.zoomScroll.aceDrawView.hidden = NO;
        self.zoomScroll.scrollEnabled = NO;
        self.editPanflag = YES;
        self.zoomScroll.panGestureRecognizer.enabled = NO;
        self.panClickscreen = YES;
        [btn setImage:[UIImage imageNamed:@"forscreen_editclick.png"] forState:UIControlStateNormal];
       TcpClient *tcp = [TcpClient sharedInstance];
         //发送初始的 颜色值
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
             for(NSString *host in self.hostIdarr){
            [tcp writeString:[Common tcpDrawingstring:[host intValue] Metuod:@"DeviceSetDrawColor" Param:self.colorPanArr[self.segmentColor] Type:@"Color"]];
             }
        });
        //发送初始的 宽度值
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
             for(NSString *host in self.hostIdarr){
            [tcp writeString:[Common tcpDrawingstring:[host intValue] Metuod:@"DeviceSetDrawWidthLevel" Param:StringFromInt(self.segmentWidth) Type:@"WidthLevel"]];
             }
        });
        //开始画图 发送tcp start 命令
 
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
             for(NSString *host in self.hostIdarr){
                 [tcp writeString:[Common tcpStartEnDevDrastring:[host intValue] Metuod:@"DeviceStartMark"]];
             }
        });
       // i = 1;
    }
    else{
        self.drawView.hidden = YES;
        self.zoomScroll.aceDrawView.drawingMode = DrawingModeNoneing;
        self.zoomScroll.panflag = NoPan;
        self.zoomScroll.pinchGestureRecognizer.enabled = YES;
        self.zoomScroll.scrollEnabled = YES;
        self.zoomScroll.panGestureRecognizer.enabled = YES;
        self.panClickscreen = NO;
        self.editPanflag = NO;
        if(!self.panClickscreen && !self.forBtnscreen){
            self.zoomScroll.aceDrawView.hidden = NO;
            self.panClickscreen = NO;
            self.forBtnscreen = NO;
        }
        [btn setImage:[UIImage imageNamed:@"forscreen_edit.png"] forState:UIControlStateNormal];
        //结束画图发送 end drawing 命令
        TcpClient *tcp = [TcpClient sharedInstance];
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
             for(NSString *host in self.hostIdarr){
            [tcp writeString:[Common tcpStartEnDevDrastring:[host intValue] Metuod:@"DeviceEndMark"]];
             }
        });
       // i = 0;
    }
}
-(IBAction)allcolorChange:(id)sender
{
    self.zoomScroll.aceDrawView.drawingMode = DrawingModePainting;
    UIButton *btn = (UIButton *)sender;
    self.zoomScroll.aceDrawView.lineColor = btn.backgroundColor;
    self.chooseColour.hidden = NO;
    self.chooseWidth.hidden = NO;
    CGRect r = self.chooseColour.frame;
    r.origin.x = 20 + (btn.tag * 54);
    self.chooseColour.frame = r;
    self.segmentColor = btn.tag;
    //发送设置画笔颜色的命令
    TcpClient *tcp = [TcpClient sharedInstance];
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for(NSString *host in self.hostIdarr){
        [tcp writeString:[Common tcpDrawingstring:[host intValue] Metuod:@"DeviceSetDrawColor" Param:self.colorPanArr[btn.tag] Type:@"Color"]];
        }
    });
}
-(IBAction)allwidthLine:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    CGFloat f = (CGFloat)(btn.tag*2+4);
    self.segmentWidth = btn.tag;
    self.zoomScroll.aceDrawView.lineWidth = f;
    self.chooseWidth.hidden = NO;
    self.chooseColour.hidden = NO;
    CGRect r = self.chooseWidth.frame;
    r.origin.x = 359 + (btn.tag * 62);
    self.chooseWidth.frame = r;
    //发送设置画笔宽度的命令
    TcpClient *tcp = [TcpClient sharedInstance];
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         for(NSString *host in self.hostIdarr){
        [tcp writeString:[Common tcpDrawingstring:[host intValue] Metuod:@"DeviceSetDrawWidthLevel" Param:StringFromInt(btn.tag) Type:@"WidthLevel"]];
         }
    });

}
-(IBAction)myRubberSeraser:(id)sender
{
    self.chooseWidth.hidden  = YES;
    self.chooseColour.hidden = YES;
    self.zoomScroll.aceDrawView.drawingMode = DrawingModeEraseing;
    //发送画刷命令
    
    
}
-(IBAction)clearDraw:(id)sender
{
   [self.zoomScroll.aceDrawView clear];
    //发送清空命令
    TcpClient *tcp = [TcpClient sharedInstance];
     for(NSString *host in self.hostIdarr){
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [tcp writeString:[Common tcpStartEnDevDrastring:[host intValue] Metuod:@"DeviceClearDraw"]];
    });
    }
}
//tap
-(void)tappro:(UIGestureRecognizer *)gesture
{
    [self.proView.view removeFromSuperview];
    [self.promeView removeFromSuperview];
    [self.messageBtn setImage:[UIImage imageNamed:@"forscreen_message.png"] forState:UIControlStateNormal];
}
#pragma -make proViewDelegate

-(void)promtblock
{
    [self.promeView removeFromSuperview];
    [self.messageBtn setImage:[UIImage imageNamed:@"forscreen_message.png"] forState:UIControlStateNormal];
}
@end
